package com.ems.controller;

import com.ems.dao.DepartmentDAO;
import com.ems.model.Department;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


public class DepartmentServlet extends HttpServlet {
    private DepartmentDAO departmentDAO;

    @Override
    public void init() {
        departmentDAO = new DepartmentDAO();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action") == null ? "list" : request.getParameter("action");
        try {
            switch (action) {
                case "new":
                    showNewForm(request, response);
                    break;
                case "edit":
                    showEditForm(request, response);
                    break;
                case "delete":
                    deleteDepartment(request, response);
                    break;
                case "tree":
                    showDepartmentTree(request, response);
                    break;
                default:
                    listDepartments(request, response);
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action") == null ? "list" : request.getParameter("action");
        try {
            switch (action) {
                case "insert":
                    insertDepartment(request, response);
                    break;
                case "update":
                    updateDepartment(request, response);
                    break;
                default:
                    listDepartments(request, response);
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }

    private void listDepartments(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        List<Department> departments = departmentDAO.getAllDepartments();
        request.setAttribute("departments", departments);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/department/list.jsp");
        dispatcher.forward(request, response);
    }

    private void showDepartmentTree(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        List<Department> departments = departmentDAO.getAllDepartments();
        request.setAttribute("departments", departments);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/department/tree.jsp");
        dispatcher.forward(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            request.setAttribute("departments", departmentDAO.getAllDepartments());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/department/form.jsp");
        dispatcher.forward(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Department existingDepartment = departmentDAO.getDepartmentById(id);
        request.setAttribute("department", existingDepartment);
        request.setAttribute("departments", departmentDAO.getAllDepartments());
        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/department/form.jsp");
        dispatcher.forward(request, response);
    }

    private void insertDepartment(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        Department newDepartment = new Department();
        populateDepartmentFromRequest(request, newDepartment);
        departmentDAO.addDepartment(newDepartment);
        response.sendRedirect("department");
    }

    private void updateDepartment(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Department department = departmentDAO.getDepartmentById(id);
        populateDepartmentFromRequest(request, department);
        departmentDAO.updateDepartment(department);
        response.sendRedirect("department");
    }

    private void deleteDepartment(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        departmentDAO.deleteDepartment(id);
        response.sendRedirect("department");
    }

    private void populateDepartmentFromRequest(HttpServletRequest request, Department department) {
        department.setName(request.getParameter("name"));
        department.setDescription(request.getParameter("description"));
        String parentIdStr = request.getParameter("parentId");
        int parentId = parentIdStr == null || parentIdStr.isEmpty() ? 0 : Integer.parseInt(parentIdStr);
        department.setParentId(parentId);
    }
}
